﻿@using NITASA.Data
@using NITASA.Areas.Admin.Helper
@using System.Web.Mvc
@{
    ViewBag.Title = "Template Editor";
}

<style>
    tfoot td {
        line-height: 3 !important;
    }

    .custom-pagination td {
        padding-left: 10px !important;
    }

    .table-normal tbody td.col2 {
        width: 50px;
        text-align: center;
    }

    .table-normal tbody td.col3 {
        width: 115px;
        text-align: center;
    }
    .padbottom10 {padding-bottom:10px;
    } .code-file-editor {
        width: 100%;
        height: 657px;
    }
    #dvfilename {font-size:20px;font-weight:700;color:burlywood;}
</style>
<div class="main-content">
    <div class="container-fluid padded">
        @if (TempData["SuccessMessage"] != null)
        {
            <div class="alert alert-success">
                <button class="close" data-dismiss="alert" type="button">
                    ×
                </button>
                @TempData["SuccessMessage"]
            </div>
        }
        @if (TempData["ErrorMessage"] != null)
        {
            <div class="alert alert-error">
                <button class="close" data-dismiss="alert" type="button">
                    ×
                </button>
                @TempData["ErrorMessage"]
            </div>
        }
        <div class="row-fluid">
            <div class="span3">
                <div class="box">
                    <div class="box-header">
                        <span class="title">Select File Type</span>
                    </div>
                    <div class="box-content">
                        <div class="padded">
    @Html.DropDownList("ddlFileType", (IEnumerable<SelectListItem>)ViewBag.typeList, "All")                        
    @*@using (Html.BeginForm("GetFiles", "Template", FormMethod.Get))
                            {
                                <table class="form-horizontal fill-up validatable width100per">
                                  
                                    <tr>
                                        <td class="width70per">
                                            
                                        </td>
                                    </tr>
                                   
                                </table>
                            }*@
                        </div>
                    </div>
                </div>
                <div class="box">
                    <div class="box-header">
                        <span class="title">Select File</span>
                    </div>
                    <div class="box-content">
                        <div class="padded">
                            @using (Html.BeginForm("GetFiles", "Template", FormMethod.Get))
                            {
                                @Html.DropDownList("ddlFile", (List<SelectListItem>)ViewData["fileList"], new { style = "height:400px;", size = 4 })
                                @*<table class="form-horizontal fill-up validatable width100per">
                                    <tr>
                                        <td class="width70per">
                                        </td>
                                    </tr>
                                </table>*@
                            }
                        </div>
                    </div>
                </div>
            </div>
            <div class="span9">
                @using (Html.BeginForm("SaveFile", "Template", FormMethod.Post))
                {
                    <div id="divFileInfo">
                        <div class="span10" id="dvfilename"></div>
                        <div class="span2 pull-right padbottom10"><input type="submit" class="btn btn-blue pull-right" value="Save" /></div>
                    </div>
                    <div>
                        @Html.TextArea("txtFileContent", new { rows = "100", cols = "150", @class = "code-file-editor" })
                        <input type="hidden" value=@ViewData["lastupdatedfile"] id="hdnfilename" name="hdnfilename" />
                    </div>
                }
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        if ($("#hdnfilename").val() != '') {
            $('#ddlFile option[value="' + $("#hdnfilename").val() + '"]').attr('selected', 'selected');
            $('#dvfilename').text($("#hdnfilename").val());
            $("#divFileInfo").show();
        }
        else {
            $("#divFileInfo").hide();
        }
        $("#ddlFileType").change(function () {
            var ddlFileType = $(this).val();
            $.ajax({
                type: "GET",
                url: '/Admin/Template/GetFiles',
                data: { fileType: ddlFileType },
                cache: false,
                dataType: "text",
                success: function (data) {
                    $('#ddlFile').empty()
                    if (data != false) {
                        var array = data.split(',');
                        $.each(array, function (key, value) {
                            console.log(key, value);
                            $('#ddlFile')
                                .append($("<option></option>")
                                .attr("value", value)
                                .text(value));
                        });
                    }
                    else {
                        //$('#txtFileContent').val(data);
                    }
                }
            });
        });

        $("#ddlFile").change(function () {
            var fileName = $('#ddlFile').val();
            $('#hdnfilename').val(fileName);
            $('#hdnfilename').text(fileName);
            $('#dvfilename').text(fileName);
            $.ajax({
                type: "GET",
                url: '/Admin/Template/FileDataContent',
                data: { filePath: fileName },
                cache: false,
                dataType: "text",
                success: function (data) {
                    if (data != false) {
                        $('#txtFileContent').val(data);
                    }
                    else {
                        $('#txtFileContent').val(data);
                    }
                    $("#divFileInfo").show();
                }
            });
        });
    });

</script>